<?php


namespace App\Model;


use App\Tool\Json;

/**
 * App\Model\OrderGoods
 *
 * @property int $id
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property int $order_id 订单id号
 * @property int $goods_id 商品id
 * @property int $goods_sku_id skuid
 * @property int $seller_id 商铺id
 * @property string $goods_name 商品名称
 * @property string $num 商品数量
 * @property string $sku_msg sku信息
 * @property float $price 商品价格
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\OrderGoods newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\OrderGoods newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\OrderGoods query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\OrderGoods whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\OrderGoods whereGoodsId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\OrderGoods whereGoodsName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\OrderGoods whereGoodsSkuId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\OrderGoods whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\OrderGoods whereNum($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\OrderGoods whereOrderId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\OrderGoods wherePrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\OrderGoods whereSellerId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\OrderGoods whereSkuMsg($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\OrderGoods whereUpdatedAt($value)
 * @mixin \Eloquent
 * @property-read \App\Model\GoodsSku $goods_sku
 * @property-read \App\Model\Order $order
 * @property int $snapped_sku_id 秒杀商品规格id,为0是非秒杀商品
 * @property-read \App\Model\SnappedSku $snapped_sku
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\OrderGoods whereSnappedSkuId($value)
 */
class OrderGoods extends Base
{

    protected $table='order_goods';

    protected $fillable=['order_id','goods_id','goods_sku_id','seller_id','goods_name','num','sku_msg',
        'price','snapped_sku_id'];

//    protected $appends

    function goods_sku(){


        return $this->belongsTo(GoodsSku::class,'goods_sku_id','id');
    }

    function order(){


        return $this->belongsTo(Order::class,'order_id','id');
    }


    function snapped_sku(){

        return $this->belongsTo(SnappedSku::class,'snapped_sku_id','id');
    }

    function goods(){


        return $this->belongsTo(Goods::class,'goods_id','id');
    }

    /**
     * 商品退款状态，1是无退款和已驳回（商品正常状态），2是正在退款申请和退款中（商品正在退款审核流程），3是已退款
     * Create by PeterYang
     * 2020/07/04 11:37:00
     * Email:904801074@qq.com
     */
    function getRefundStatusAttribute()
    {

        $order_goods_id=$this->attributes['id'];



        $is_refund=\App\Model\Refund::where('order_goods_id', $order_goods_id)
            ->where('user_id', resolve('weapp_user')['id'])->orderBy('id','desc')->first();



        if($is_refund){
//            $data->order_goods[$k]['is_refund']=true;
            if($is_refund->status==1||$is_refund->status==2) return 2;

            if($is_refund->status==3) return 3;

            if($is_refund->status==4) return 1;

        }else{

            return 1;
//            $data->order_goods[$k]['is_refund']=false;
        }

    }

}
